package com.ruoyix.novel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyix.common.utils.StringUtils;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyix.novel.domain.bo.NovelChapterOutlineBo;
import com.ruoyix.novel.domain.vo.NovelChapterOutlineVo;
import com.ruoyix.novel.domain.NovelChapterOutline;
import com.ruoyix.novel.mapper.NovelChapterOutlineMapper;
import com.ruoyix.novel.service.INovelChapterOutlineService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 章节大纲Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-05
 */
@RequiredArgsConstructor
@Service
public class NovelChapterOutlineServiceImpl implements INovelChapterOutlineService {

    private final NovelChapterOutlineMapper baseMapper;

    /**
     * 查询章节大纲
     */
    @Override
    public NovelChapterOutlineVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询章节大纲列表
     */
    @Override
    public TableDataInfo<NovelChapterOutlineVo> queryPageList(NovelChapterOutlineBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NovelChapterOutline> lqw = buildQueryWrapper(bo);
        Page<NovelChapterOutlineVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询章节大纲列表
     */
    @Override
    public List<NovelChapterOutlineVo> queryList(NovelChapterOutlineBo bo) {
        LambdaQueryWrapper<NovelChapterOutline> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NovelChapterOutline> buildQueryWrapper(NovelChapterOutlineBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NovelChapterOutline> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getVolumeId() != null, NovelChapterOutline::getVolumeId, bo.getVolumeId());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), NovelChapterOutline::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getSummary()), NovelChapterOutline::getSummary, bo.getSummary());
        lqw.eq(bo.getParentId() != null, NovelChapterOutline::getParentId, bo.getParentId());
        lqw.eq(bo.getOrderIndex() != null, NovelChapterOutline::getOrderIndex, bo.getOrderIndex());
        return lqw;
    }

    /**
     * 新增章节大纲
     */
    @Override
    public Boolean insertByBo(NovelChapterOutlineBo bo) {
        NovelChapterOutline add = BeanUtil.toBean(bo, NovelChapterOutline.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改章节大纲
     */
    @Override
    public Boolean updateByBo(NovelChapterOutlineBo bo) {
        NovelChapterOutline update = BeanUtil.toBean(bo, NovelChapterOutline.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NovelChapterOutline entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除章节大纲
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
